package com.jn.glide.views;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jn.glide.R;
import com.jn.glide.adapter.MovieAdapter;
import com.jn.glide.base.BaseActivity;
import com.jn.glide.contract.MovieContract;
import com.jn.glide.entity.MovieEntity;
import com.jn.glide.presenter.MoviePresenter;
import com.jn.glide.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class MovieActivity extends BaseActivity implements MovieContract.IMovieView, MovieAdapter.OnItemClickListener {
    private RecyclerView mRecyclerView;
    private MovieContract.IMoviePresenter mPresenter;
    private MovieAdapter mAdapter;
    private List<MovieEntity.DataDTO> mList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_movie;
    }

    @Override
    public void init() {
        mRecyclerView = findViewById(R.id.recyclerView);
        mPresenter = new MoviePresenter(this);
        LinearLayoutManager linearLayoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new MovieAdapter(mList, this);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.addItemClickListener(this);
        mPresenter.getData();
    }

    @Override
    public void showData(MovieEntity movie) {
        if (movie != null && movie.getData() != null) {
            LogUtils.d("asdf", "pic 1 : " + movie.getData().get(0).getPic());
            mList = movie.getData();
            mAdapter.updateData(mList);
        } else {
            showToast("未查到数据，请输入正确单号！");
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {

    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }

    @Override
    public void onItemClick(String url) {
        Glide.with(this)
                .asBitmap()
                .load(url)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        saveImageToGallery(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    public void saveImageToGallery(Bitmap bitmap) {
        String savedImageURL = MediaStore.Images.Media.insertImage(
                getContentResolver(),
                bitmap,
                "avatar",
                "avatar save"
        );
        showToast("保存成功! URL:" + savedImageURL);
    }
}